layui.config({
    base: '/plugins/module/'
});
layui.use(['table', 'layer', 'form', 'cstmodules', 'upload'], function () {
    var mymod = layui.cstmodules;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var upload = layui.upload;
    var mainIndex = null;
    upload.render({
        elem: '.thumbBox'
        , url: '/file/uploadFile'
        , acceptMime: 'image/*'
        , field: 'mf'
        , method: "post"  //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
        , done: function (res, index, upload) {
            var path = res.path;
            ///upload/img/2021-01-06/E3FC668F09E0402DB2B7578CFE92627D.jpg
            console.log(path)
            $('.thumbImg').attr('src', '/file/showImageByPath?path=' + path);
            $('#img_url').val(path);
            $('.thumbBox').css("background", "#fff");
        }
    });
    var LevleConifg = {
        param: {
            tableColumn: [
                [
                    {type: 'checkbox'},
                    {field: 'id', sort: true, title: 'ID', align: 'center'}
                    , {field: 'name', title: '抽奖等级', align: 'center'}
                    , {field: 'ratio', title: '抽奖比例', align: 'center'}
                    , {title: '操作', toolbar: '#id_rewardLevel', align: 'center'}
                ]
            ],
            limit: 15,
            limits: [15, 30, 60, 120]
        },

        //删除
        removeRewardLevel: function (data) {
            layer.confirm('你确定要删除【' + data.name + '】此抽奖等级?', {icon: 3, title: '提示'}, function (index) {
                $.post("/reward/deleteRewardLevel", {id: data.id}, function (res) {
                    if (res.code == 1) {
                        table.reload('tab_rewardLevel');
                    }
                    layer.msg(res.info);
                })
                layer.close(index);
            })
        },

        modifyRewardLevel: function (data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#modifyRewardLevel"),
                area: ['650px', '500px'],
                title: '修改等级',
                skin: "layui-layer-lan",
                success: function () {
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm", data);
                    url = "/reward/updateRewardLevel";
                    console.log(data.img_url)
                    $('.thumbImg').attr('src', '/file/showImageByPath?path=' + data.img_url);
                }
            });
        },

        //批量删除
        batchDelete: function () {
            //得到选中行
            var checkStatus = table.checkStatus('tab_rewardLevel');
            var dataLength = checkStatus.data.length;
            if (dataLength > 0) {
                layer.confirm('你确定要删除这些数据吗?', {icon: 3, title: '提示'}, function (index) {
                    var data = checkStatus.data; //获取选中行的数据
                    var ids = "";
                    $.each(data, function (index, item) {
                        if (index == 0) {
                            ids += "ids=" + item.id;
                        } else {
                            ids += "&ids=" + item.id;
                        }
                    })
                    console.log(ids)
                    $.post("/reward/batchDeleteLevel", ids, function (res) {
                        if (res.code >= 1) {
                            layer.msg(res.msg);
                            table.reload('tab_rewardLevel');
                        } else {
                            layer.msg('删除失败！');
                        }
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                });
            } else {
                layer.msg("请选中操作行")
            }
        },

        addRewardLevel: function (data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#modifyRewardLevel"),
                area: ['650px', '500px'],
                title: '添加等级',
                skin: "layui-layer-lan",
                success: function () {
                    $("#dataFrm")[0].reset();
                    url = "/reward/insertRewardLevel";

                    $('.thumbImg').attr('src', '/file/showImageByPath?path=images/meinv.jpg');
                    $('#goodsimg').val("images/meinv.jpg");
                }
            });
        }
    };

    mymod.initPageTableMethod(table, "tab_rewardLevel", "/reward/loadRewardLevel", LevleConifg.param.tableColumn, "#addLevel", null, LevleConifg.param.limit, LevleConifg.param.limits, 75);

    //监听工具条
    table.on('tool(f-rewardLevel)', function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'delete':
                LevleConifg.removeRewardLevel(data);
                break;
            case 'modify':
                LevleConifg.modifyRewardLevel(data);
                break;
        }
        ;
    });


    //监听提交
    form.on('submit(doSubmit)', function (data) {
        console.log("data====》" + data.field)
        $.post(url, data.field, function (res) {
            if (res.code === 1) {
                table.reload('tab_rewardLevel');
            }
            layer.close(mainIndex);
            layer.msg(res.info);
        })
        return false;
    });

    //监听头部工具栏
    table.on('toolbar(f-rewardLevel)', function (obj) {
        switch (obj.event) {
            case 'add':
                LevleConifg.addRewardLevel();
                break;
            case 'batchDelete':
                LevleConifg.batchDelete();
        }
        ;
    });
});
